﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class ContragentsDetails : Form
    {
        private Contragents _parent;
        public DataSets.Dictionary.ContragentsDataTable ContragentsData
        {
            get
            {
                if (_parent == null)
                    return null;
                return _parent.dictionary.Contragents;
            }
        }
        private DataSets.Dictionary.ContragentsRow _curRow;

        public DataSets.Dictionary.ContragentsRow CurrentRow
        {
            get { return _curRow; }
            set 
            {
                if (value == null)
                    throw new Exception("Не найдена запись в таблице контрагентов");
                _curRow = value; 
            }
        }
        private Util.ActionType _openAction;

        public Util.ActionType OpenAction
        {
            get { return _openAction; }
            set { _openAction = value; }
        }
        private Int32 _contragentId;

        public Int32 ContragentId
        {
            get { return _contragentId; }
            set { _contragentId = value; }
        }


        public ContragentsDetails()
        {
            InitializeComponent();
        }
        public ContragentsDetails(Util.ActionType actType, Int32 contrId, Contragents parent)
        {
            OpenAction = actType;
            ContragentId = contrId;
            _parent = parent;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ContragentsDetails_Load(object sender, EventArgs e)
        {
            switch (OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                default:
                    this.Close();
                    break;
            }
        }
        private void InitInsert()
        {
            btnSaveUpdate.Text = "Сохранить";
            btnDelete.Visible = false;
        }
        private void InitUpdate()
        {
            btnDelete.Visible = true;
            btnSaveUpdate.Text = "Изменить";
            this.CurrentRow = ContragentsData.FindById(ContragentId);
            txbAdress.Text = CurrentRow.ActualAddress;
            txbBank.Text = CurrentRow.Bank;
            txbBIK.Text = CurrentRow.BIK;
            txbCorrespondentAccount.Text = CurrentRow.CorrespondentAccount;
            txbEmail.Text = CurrentRow.EMail;
            txbFax.Text = CurrentRow.Fax;
            txbFirmType.Text = CurrentRow.FirmType;
            txbICQ.Text = CurrentRow.ICQ;
            txbINN.Text = CurrentRow.INN;
            txbJuridikalAdres.Text = CurrentRow.JuridicalAddress;
            txbKPP.Text = CurrentRow.KPP;
            txbName.Text = CurrentRow.Name;
            txbNote.Text = CurrentRow.Note;
            txbOKONX.Text = CurrentRow.OKONX;
            txbOKPO.Text = CurrentRow.OKPO;
            txbPhone.Text = CurrentRow.Phone;
            txbSettlementAccount.Text = CurrentRow.SettlementAccount;
            txbShortName.Text = CurrentRow.ShortName;
            txbWeb.Text = CurrentRow.WebPage;
            cbCustomer.Checked = CurrentRow.IsCustomer;
            cbSeller.Checked = CurrentRow.IsSeller;
            cbSertificated.Checked = CurrentRow.IsSertification;
            txbDirectorFio.Text = CurrentRow.DirectorFIO;
            txbDirectorPosition.Text = CurrentRow.DirectorPosition;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите удалить контрагента " + CurrentRow.Name + "?", "Удаление контрагента", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                // DIProject.DataSets.Dictionary.CatalogDataTable cat = this.CatalogData;
                CurrentRow.MW = FirstForm.Login;
                CurrentRow.Delete();
                DIProject.DataSets.DictionaryTableAdapters.ContragentsTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.ContragentsTableAdapter();
                adapter.Update(ContragentsData);
                ContragentsData.AcceptChanges();
                adapter.Fill(ContragentsData);
            }
            this.Close();
        }

        private void btnSaveUpdate_Click(object sender, EventArgs e)
        {
            DataSets.DictionaryTableAdapters.ContragentsTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.ContragentsTableAdapter();
            if (OpenAction == DIProject.Util.ActionType.Insert)
            {
                DataSets.Dictionary.ContragentsRow cr = ContragentsData.NewContragentsRow();
                cr.ActualAddress = txbAdress.Text;
                cr.Bank = txbBank.Text;
                cr.BIK = txbBIK.Text;
                cr.CorrespondentAccount = txbCorrespondentAccount.Text;
                cr.EMail = txbEmail.Text;
                cr.Fax = txbFax.Text;
                cr.FirmType = txbFirmType.Text;
                cr.ICQ = txbICQ.Text;
                cr.INN = txbINN.Text;
                cr.JuridicalAddress = txbJuridikalAdres.Text;
                cr.KPP = txbKPP.Text;
                cr.Name = txbName.Text;
                cr.Note = txbNote.Text;
                cr.OKONX = txbOKONX.Text;
                cr.OKPO = txbOKPO.Text;
                cr.Phone = txbPhone.Text;
                cr.SettlementAccount = txbSettlementAccount.Text;
                cr.ShortName = txbShortName.Text;
                cr.WebPage = txbWeb.Text;
                cr.IsCustomer = cbCustomer.Checked;
                cr.IsSeller = cbSeller.Checked;
                cr.IsSertification = cbSertificated.Checked;
                cr.MD = DateTime.Now;
                cr.DirectorFIO = txbDirectorFio.Text;
                cr.DirectorPosition = txbDirectorPosition.Text;
                cr.ToDelete = false;
                string MW = FirstForm.Login;
                if (string.IsNullOrEmpty(MW))
                    MW = string.Empty;
                cr.MW = MW;
                ContragentsData.Rows.Add(cr);
                adapter.Update(ContragentsData);
                ContragentsData.AcceptChanges();

            }
            else
                if (OpenAction == DIProject.Util.ActionType.Update)
                {
                    CurrentRow.BeginEdit();
                    CurrentRow.ActualAddress = txbAdress.Text;
                    CurrentRow.Bank = txbBank.Text;
                    CurrentRow.BIK = txbBIK.Text;
                    CurrentRow.CorrespondentAccount = txbCorrespondentAccount.Text;
                    CurrentRow.EMail = txbEmail.Text;
                    CurrentRow.Fax = txbFax.Text;
                    CurrentRow.FirmType = txbFirmType.Text;
                    CurrentRow.ICQ = txbICQ.Text;
                    CurrentRow.INN = txbINN.Text;
                    CurrentRow.JuridicalAddress = txbJuridikalAdres.Text;
                    CurrentRow.KPP = txbKPP.Text;
                    CurrentRow.Name = txbName.Text;
                    CurrentRow.Note = txbNote.Text;
                   // CurrentRow.OId = -1;
                    CurrentRow.OKONX = txbOKONX.Text;
                    CurrentRow.OKPO = txbOKPO.Text;
                    CurrentRow.Phone = txbPhone.Text;
                    CurrentRow.SettlementAccount = txbSettlementAccount.Text;
                    CurrentRow.ShortName = txbShortName.Text;
                    CurrentRow.WebPage = txbWeb.Text;
                    CurrentRow.IsCustomer = cbCustomer.Checked;
                    CurrentRow.IsSeller = cbSeller.Checked;
                    CurrentRow.IsSertification = cbSertificated.Checked;
                    CurrentRow.MD = DateTime.Now;
                    CurrentRow.DirectorFIO = txbDirectorFio.Text;
                    CurrentRow.DirectorPosition = txbDirectorPosition.Text;
                    CurrentRow.ToDelete = false;
                    CurrentRow.EndEdit();
                    adapter.Update(CurrentRow);
                    ContragentsData.AcceptChanges();
                }
                else
                {
                    this.Close();
                }
            this.Close();
        }
    }
}